package com.fgw.controller.portal;

import com.fgw.common.Const;
import com.fgw.common.ServerResponse;
import com.fgw.pojo.Teacher;
import com.fgw.service.ITeacherlibService;
import com.fgw.vo.TeacherlibVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by lcx on 2017/9/13.
 */
@Controller
@RequestMapping("/Teacherlib/")
public class TeacherlibController {

    @Autowired
    private ITeacherlibService iTeacherlibService;

    /**
     * 首页师资库的Controller
     * @return
     */
    @RequestMapping(value = "list.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<TeacherlibVo> getTeacherlibList(){
        return iTeacherlibService.getTeacherlibList();
    }

    /**
     * 师资登录Controller
     * @param tuser
     * @param tpassword
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Teacher> login(String tuser, String tpassword, HttpSession session){

        ServerResponse<Teacher> response=iTeacherlibService.login(tuser,tpassword);

        if(response.isSuccess()){
            //设置前端回显数据
            session.setAttribute(Const.CURRENT_USER,response.getData());
            session.setAttribute(Const.TSPECIALTY,response.getData());
        }
        return response;
    }

    /**
     * 师资登出Controller
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public  ServerResponse<String> logout(HttpSession session){
        //数据移除
        session.removeAttribute(Const.CURRENT_USER);
        session.removeAttribute(Const.TSPECIALTY);
        return ServerResponse.createBySuccess();
    }

    /**
     *  检查登录状态
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Teacher> getUserInfo(HttpSession session) {
        //判断用户是否登录
        Teacher teacher = (Teacher) session.getAttribute(Const.CURRENT_USER);
        if (teacher != null) {
            //如果登录则从session中取出user
            return ServerResponse.createBySuccess(teacher);
        }

        return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
    }


}
